package pl.edu.pk.cc.server.main;

import java.lang.reflect.Method;

import org.jboss.logging.Logger;

import pl.edu.pk.cc.common.util.CCServerRequest;

public class CCServletRequestInterpreter {

	private static final Logger logg = Logger.getLogger(CCServletRequestInterpreter.class);

	public static Object process(CCServerRequest clientRequest) {

		try {
			Class<?> requestedClass = Class.forName(clientRequest.getClassName());
			Object results = null;

			if (CCServerSessionsManager.checkSession(clientRequest.getSessionID()) && clientRequest.getParams() != null) {
				Method requestedMethod = requestedClass.getMethod(clientRequest.getMethod(), clientRequest.getParams().getClass());
				results = requestedMethod.invoke(null, clientRequest.getParams());
				logg.info(requestedMethod.getName() + " method invoked");
			} else {
				Method requestedMethod = requestedClass.getMethod(clientRequest.getMethod());
				results = requestedMethod.invoke(null);
				logg.info(requestedMethod.getName() + " method invoked");
			}

			return results;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;

	}

}
